package com.wwp.tbshop.activity;

import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.itheima.pulltorefreshlib.PullToRefreshBase;
import com.itheima.pulltorefreshlib.PullToRefreshListView;
import com.wwp.tbshop.R;
import com.wwp.tbshop.model.bean.SearchListBean;
import com.wwp.tbshop.presenter.SearchPresenter;
import com.wwp.tbshop.util.UiUtil;
import com.wwp.tbshop.view.adapter.SearchRVAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by Xiaokun on 2017/6/18.
 */

public class SearchToActivity extends BaseActivity {


    @Bind(R.id.iv_searchto_back)
    ImageView ivSearchtoBack;
    @Bind(R.id.et_search_to)
    EditText etSearchTo;
    @Bind(R.id.tv_search_to)
    TextView tvSearchTo;
    @Bind(R.id.iv_searchto_delete)
    ImageView ivSearchtoDelete;
    @Bind(R.id.rv_search_listview)
    PullToRefreshListView rvSearchListview;
    @Bind(R.id.tv_search_sale)
    TextView tvSearchSale;
    @Bind(R.id.tv_search_price)
    TextView tvSearchPrice;
    @Bind(R.id.tv_search_comment)
    TextView tvSearchComment;
    @Bind(R.id.tv_search_shelves)
    TextView tvSearchShelves;

    private SearchRVAdapter mSearchRVAdapter;
    private int PAGE = 1;
    private SearchPresenter mPresenter;
    private String mKeyword;
    private List<SearchListBean.ProductListBean> mProductList = new ArrayList<>();

    private String defaultOrderby = "saleDown";
    private boolean isPriceUp = true;

    @Override
    public int getLayoutId() {
        return R.layout.activity_search_to;
    }

    @Override
    protected void init() {
        initView();
        loadData();
    }

    private void initView() {
        Intent intent = getIntent();
        String search_keyword = intent.getStringExtra("search_keyword");
        etSearchTo.addTextChangedListener(textChangedListener);
        if (search_keyword != null) {
            etSearchTo.setText(search_keyword);
        }

        mSearchRVAdapter = new SearchRVAdapter(this);

        rvSearchListview.setAdapter(mSearchRVAdapter);
        rvSearchListview.setMode(PullToRefreshBase.Mode.PULL_FROM_END);
        rvSearchListview.setOnRefreshListener(refreshListener);

        rvSearchListview.setOnItemClickListener(itemClickListener);
    }

    private AdapterView.OnItemClickListener itemClickListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Log.e("click", "onItemClick:" + mProductList.get(position - 1).getId());
            Intent intent=new Intent(UiUtil.getContext(),ProductDetailActivity.class);
            intent.putExtra("pId",mProductList.get(position-1).getId());
            startActivity(intent);
        }
    };

    private PullToRefreshBase.OnRefreshListener<ListView> refreshListener = new PullToRefreshBase.OnRefreshListener<ListView>() {
        @Override
        public void onRefresh(PullToRefreshBase<ListView> pullToRefreshBase) {
            rvSearchListview.postDelayed(new Runnable() {
                @Override
                public void run() {
                    PAGE++;
                    Log.e("page", "run: " + PAGE);
                    mPresenter.searchGoods(mKeyword, PAGE, 10, defaultOrderby);
                    rvSearchListview.onRefreshComplete();
                }
            }, 1000);
        }
    };

    private TextWatcher textChangedListener = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            if (TextUtils.isEmpty(s)) {
                ivSearchtoDelete.setVisibility(View.GONE);
            } else {
                ivSearchtoDelete.setVisibility(View.VISIBLE);
            }
        }
    };

    @OnClick({R.id.iv_searchto_back, R.id.tv_search_to, R.id.iv_searchto_delete, R.id.tv_search_sale, R.id.tv_search_price, R.id.tv_search_comment, R.id.tv_search_shelves})
    public void onViewClicked(View view) {

        switch (view.getId()) {
            case R.id.iv_searchto_back:
                finish();
                break;
            case R.id.tv_search_to:
                mProductList.clear();
                loadData();
                break;
            case R.id.iv_searchto_delete:
                clearText();
                break;
            case R.id.tv_search_sale:
                loadDataByOrderby("saleDown");
                defaultOrderby = "saleDown";
                break;
            case R.id.tv_search_price:
                if (isPriceUp) {
                    loadDataByOrderby("priceUp");
                    defaultOrderby = "priceUp";
                } else {
                    loadDataByOrderby("priceDown");
                    defaultOrderby = "priceDown";
                }
                isPriceUp = !isPriceUp;
                break;
            case R.id.tv_search_comment:
                loadDataByOrderby("commentDown");
                defaultOrderby = "commentDown";
                break;
            case R.id.tv_search_shelves:
                loadDataByOrderby("shelvesDown");
                defaultOrderby = "shelvesDown";
                break;

        }
    }

    private void clearText() {
        etSearchTo.setText("");
    }

    private void loadData() {
        mKeyword = etSearchTo.getText().toString().trim();
        if (!TextUtils.isEmpty(mKeyword)) {
            mPresenter = new SearchPresenter(this);
            mPresenter.searchGoods(mKeyword, PAGE, 10, defaultOrderby);
        }
    }

    private void loadDataByOrderby(String orderby) {

        mKeyword = etSearchTo.getText().toString().trim();
        if (!TextUtils.isEmpty(mKeyword)) {
            mPresenter = new SearchPresenter(this);
            mPresenter.searchGoods(mKeyword, 1, 10, orderby);
        }
        mProductList.clear();
    }

    public void setProductList(List<SearchListBean.ProductListBean> productList) {
        if (productList != null&&productList.size()>0) {
            mProductList.addAll(productList);
            Log.e("page", mProductList.size() + "");
            mSearchRVAdapter.setData(mProductList);
        }else {
            rvSearchListview.setMode(PullToRefreshBase.Mode.DISABLED);
            Toast.makeText(this,"没有更多",Toast.LENGTH_SHORT).show();
        }
        mSearchRVAdapter.notifyDataSetChanged();
    }
}
